const app=getApp();
Component({
  data:{
    name:"测试",
    systemInfo:{},
    app:app,
    activeTypesProduct:0,
    typesProductHeight:0,
    lastItes:{},
    link:[],//幻灯数据,
    recommend:[],//商品数据
    types:[],//分类数据
    btns:[
      {url:"",tit:"爆款专区",image:"/image/btn-1.png"},
      {url:"",tit:"砍价专区",image:"/image/btn-2.png"},
      {url:"",tit:"领优惠券",image:"/image/btn-3.png"},
      {url:"",tit:"花呗专区",image:"/image/btn-4.png"},
      {url:"",tit:"全部商品",image:"/image/btn-5.png"},
    ]
  },
  props:{

  },
  didMount() {
    // 底部品质保证
    this.setData({
      lastItes:[
        {
          icon:"iconsafety_1",
          tit:"品质保证",
          title:"严格认证租赁商",
          color:"#4fadfa"
        },
        {
          icon:"iconhuabei",
          tit:"信用抵押",
          title:"超低租金0元起",
          color:"#4fadfa"
        },
        {
          icon:"iconshandian",
          tit:"快速响应",
          title:"30分钟响应服务",
          color:"#ffab2e"
        }
      ]
    })
    this._init();
  },
  didUpdate(){
    this.setData(this.props);
    // console.log("我更新了一下",this.data);
  },
  onPullDownRefresh() {
    console.log("我被下拉了")
    setTimeout(() => this.stopPullDownRefresh(), 1000);
    // 页面被下拉
  },
  stopPullDownRefresh() {
    my.stopPullDownRefresh({
      complete(res) {
        console.log("下拉完成了",res, new Date())
      }
    })
  },
  onReachBottom() {
    // 页面被拉到底部
  },
  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      title: '宜人租机',
      desc: '宜人租机',
      path: 'pages/index/index',
    };
  },
  methods:{
    // 初始化
    _init(){
      app.myRequest({
        params:{act:"zfb-shops-home"},
        success:json=>{
          const types = json.data.types.map(
            (item)=>Object.assign(
              {
               title:item.tit,
               product:[]
              },
              item
            ));
          json.data.types = types;
          const link = json.data.link.map(item=>Object.assign({},item,{image:app.getImageUrl(item.image)}));
          json.data.link = link;
          
          const recommend=json.data.recommend.map(item=>Object.assign({},item,{price:app.fenToYuan(item.price),image:app.getImageUrl(item.image)}));
          json.data.recommend = recommend;

          this.setData(json.data);
          setTimeout(() => {
            this.handleTabClick({index:0});
          }, 3000);
        }
      });
      
    },
    // 点击tabs
    handleTabClick({ index }) {
      
      this.setData({activeTypesProduct: index});
      const types = this.data.types;
      if(!this.data.types[index] || (types[index].product && types[index].product.length)){
        return;
      }
      app.myRequest({
        params:{
          act:"zfb-shops-product-paging",
          tid:types[index].id,
          p:1
        },
        success:json=>{
          if(json.status!=="ok"){return;}
          json.data.data = json.data.data.map(item=>Object.assign({},item,{image:app.getImageUrl(item.image),price:app.fenToYuan(item.price)}))
          types[index].product=json.data.data;
          this.setData({
            types:types,
            typesProductHeight:json.data.data.length*340
          });
        }
      });
      
    },
    // 点击更多
    handlePlusClick(){
      my.navigateTo({url:"/pages/types/types?tid=".concat(this.data.types[this.data.activeTypesProduct].id)});
    },
    // 跳转
    navigateTo(e){
      // my.navigateTo(e.currentTarget.dataset);
      app.navigateTo(e);
    }
  }
  
});
